package com.hilog.productservice.config;

import com.hilog.common.TraceConstants;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.MDC;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Feign配置类 - 确保traceId在服务间调用时正确传递
 */
@Configuration
public class FeignConfig {

    @Bean
    public RequestInterceptor traceIdInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate template) {
                // 从MDC中获取traceId
                String traceId = MDC.get(TraceConstants.TRACE_ID_KEY);
                if (traceId != null && !traceId.trim().isEmpty()) {
                    // 将traceId添加到请求头中
                    template.header(TraceConstants.TRACE_ID_HEADER, traceId);
                }
            }
        };
    }
} 